<template>
  <div class="attendance-list">
    <div class="breadcrumb">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item to="Home" :replace="true">首页</el-breadcrumb-item>
        <el-breadcrumb-item>考勤信息列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-form :inline="true" :model="searchParams">
      <el-form-item label="员工姓名" prop="employeeName">
        <el-input v-model="searchParams.employeeName" placeholder="请选择员工姓名" readonly="readonly">
          <i slot="suffix" class="el-input__icon el-icon-search" @click="findEmployee"></i>
        </el-input>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="searchParams.status" placeholder="请选择状态">
          <el-option label="正常" :value="10"></el-option>
          <el-option label="迟到" :value="20"></el-option>
          <el-option label="早退" :value="30"></el-option>
          <el-option label="旷工" :value="40"></el-option>
          <el-option label="加班" :value="50"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button type="primary" @click="onAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pageData.list"
      border
      v-loading="pageData.loading"
      size="mini"
      header-cell-class-name="table-header-cell-font"
      cell-class-name="table-cell-font">
      <el-table-column prop="employeeName" label="员工姓名"></el-table-column>
      <el-table-column prop="attendanceDate" label="考勤日期"></el-table-column>
      <el-table-column prop="checkInTime" label="上班打卡时间"></el-table-column>
      <el-table-column prop="checkOutTime" label="下班打卡时间"></el-table-column>
      <el-table-column prop="status" label="状态">
        <template v-slot="{ row }">
          <div v-if="row.status === 10">正常</div>
          <div v-if="row.status === 20">迟到</div>
          <div v-if="row.status === 30">早退</div>
          <div v-if="row.status === 40">旷工</div>
          <div v-if="row.status === 50">加班</div>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="{ row }">
          <el-button @click.native.prevent="editRow(row.id)" type="primary">编辑</el-button>
          <el-button @click.native.prevent="delRow(row.id)" type="danger" plain>删除</el-button>
          <el-button @click.native.prevent="detailRow(row.id)" type="primary" plain>详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="table-pagination">
      <el-pagination
        @current-change="paginationChange"
        :current-page="pageData.current"
        :page-sizes="pageData.pageSizes"
        :page-size="pageData.size"
        background
        layout="total, prev, pager, next, jumper"
        :total="pageData.count">
      </el-pagination>
    </div>
    <el-dialog :visible.sync="employeeSelectorVisible" v-if="employeeSelectorVisible" title="员工基本信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <employee-selector @employee-selected-event="handleEmployeeSelectedEvent">
      </employee-selector>
    </el-dialog>
    <el-dialog :visible.sync="attendanceAddVisible" v-if="attendanceAddVisible" :title="attendanceAddTitle" :top="dialogTop" :width="dialogWidth" :modal-append-to-body="true">
      <attendance-add :id="selectedAttendanceId" @close-attendance-add-event="handleCloseAttendanceAddEvent" @reset-attendance-add-event="handleResetAttendanceAddEvent">
      </attendance-add>
    </el-dialog>
    <el-dialog :visible.sync="attendanceViewVisible" v-if="attendanceViewVisible" title="考勤信息详情" :top="dialogTop" :width="dialogWidth">
      <attendance-view :id="selectedAttendanceId" @close-attendance-view-event="handleCloseAttendanceViewEvent">
      </attendance-view>
    </el-dialog>
  </div>
</template>

<script>
import attendanceApi from '@/api/attendanceApi'
import EmployeeSelector from "@/views/employee/EmployeeSelector.vue";
import AttendanceAdd from "@/views/attendance/AttendanceAdd.vue"
import AttendanceView from "@/views/attendance/AttendanceView.vue"
import listQueryMixin from '@/mixins/listQueryMixin'
export default {
  name: 'AttendanceList',
  components: {EmployeeSelector,AttendanceAdd, AttendanceView},
  mixins: [
    listQueryMixin
  ],
  data () {
    return {
      searchParams: {
        employeeId: '',
        employeeName: '',
        status: ''
      },
      employeeSelectorVisible: false,
      attendanceAddVisible: false,
      attendanceAddTitle: '',
      attendanceViewVisible: false,
      selectedAttendanceId: 0
    }
  },
  methods: {
    getPageData (p) {
      return attendanceApi.find(p)
    },
    onSearch () {
      this._getPageData()
    },
    onReset () {
      this.searchParams = {
        employeeId: '',
        employeeName: '',
        status: ''
      }
      this.pageData.pageIndex = 1
      this._getPageData()
    },
    onAdd () {
      this.selectedAttendanceId = ''
      this.attendanceAddVisible = true
      this.attendanceAddTitle = '考勤信息新增'
    },
    editRow (id) {
      this.selectedAttendanceId = id
      this.attendanceAddVisible = true
      this.attendanceAddTitle = '考勤信息编辑'
    },
    delRow (id) {
      if (!id) {
        return
      }
      this.$confirm('确定要删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        attendanceApi.del(id)
          .then((resp) => {
            if (resp && resp.code === 1) {
              this.$message.success('删除成功!')
              this.onSearch()
            } else {
              this.$message.error('删除失败:' + (resp.msg ? resp.msg : '原因未知'))
            }
          })
          .catch(error => {
            console.error('操作失败:', error)
          })
      }).catch(() => {})
    },
    detailRow (id) {
      this.selectedAttendanceId = id
      this.attendanceViewVisible = true
    },
    findEmployee () {
      this.employeeSelectorVisible = true
    },
    handleEmployeeSelectedEvent (selectedEmployee) {
      this.employeeSelectorVisible = false
      if (selectedEmployee && 'employeeId' in selectedEmployee && this.searchParams.employeeId !== selectedEmployee['employeeId']) {
        this.searchParams.employeeId = selectedEmployee['employeeId']
        this.searchParams.employeeName = selectedEmployee['employeeName']
      }
    },
    handleCloseAttendanceViewEvent () {
      this.attendanceViewVisible = false
    },
    handleResetAttendanceAddEvent () {
      this.attendanceAddTitle = '考勤信息新增'
    },
    handleCloseAttendanceAddEvent (params) {
      if (params && params.search) {
        this.onSearch()
      }
      this.attendanceAddVisible = false
    }
  }
}
</script>

<style lang="scss">
</style>